package com.ragdollcat.thirdaicodegenerate.controller;

import cn.hutool.core.util.ObjUtil;
import com.ragdollcat.thirdaicodegenerate.common.BaseResponse;
import com.ragdollcat.thirdaicodegenerate.common.ResultUtils;
import com.ragdollcat.thirdaicodegenerate.exception.ErrorCode;
import com.ragdollcat.thirdaicodegenerate.exception.ThrowUtils;
import com.ragdollcat.thirdaicodegenerate.model.vo.app.FileContentVO;
import com.ragdollcat.thirdaicodegenerate.model.vo.app.FileHistoryVO;
import com.ragdollcat.thirdaicodegenerate.model.vo.app.FileNodeVO;
import com.ragdollcat.thirdaicodegenerate.service.AppVersionCtlService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 应用版本控制 接口
 */
@RestController
@RequestMapping("/versionctl")
public class AppVersionCtlController {

    @Resource
    private AppVersionCtlService appVersionCtlService;

    /**
     * 根据appId 查询最新版本的文件列表
     * 用于版本控制页面，左侧边栏展示
     * @param appId 应用ID
     * @return 查询结果，如果是vue项目需要树状展示
     */
    @GetMapping("/getLatestFileList")
    public BaseResponse<FileNodeVO> getLatestFileList(Long appId, HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(appId), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(appVersionCtlService.getLatestFileList(appId,httpServletRequest));
    }

    /**
     * 根据文件名和应用id 查询具体文件的内容返回
     * 用于版本控制页面，右侧区域展示代码
     * @param appId 应用ID
     * @param fileName 文件名
     * @param httpServletRequest
     * @return 具体文件的内容
     */
    @GetMapping("/getFileContentByNameAndId")
    public BaseResponse<FileContentVO> getFileContentByNameAndId(@RequestParam("appId") Long appId,
                                                                 @RequestParam("fileName") String fileName,
                                                                 HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(appId), ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(ObjUtil.isEmpty(fileName), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(appVersionCtlService.getFileContentByNameAndId(appId,fileName,httpServletRequest));
    }

    /**
     * 根据文件名和应用id,找到某个文件的所有历史版本
     * @param appId 应用ID
     * @param fileName  文件名
     * @param httpServletRequest
     * @return  该文件所有的历史版本
     */
    @GetMapping("/showFileHistory")
    public BaseResponse<List<FileHistoryVO>> getFileHistoryByNameAndId(@RequestParam("appId") Long appId,
                                                         @RequestParam("fileName") String fileName,
                                                         HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(appId), ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(ObjUtil.isEmpty(fileName), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(appVersionCtlService.getFileHistoryByNameAndId(appId,fileName,httpServletRequest));
    }


    /**
     * 根据文件名和应用id,具体的版本 查询具体文件的内容返回
     * 用于版本控制页面，进行版本比对
     * @param appId 应用ID
     * @param fileName 文件名
     * @param version 具体的版本号
     * @param httpServletRequest
     * @return 具体文件的内容
     */
    @RequestMapping("/getFileContentByVersion")
    public BaseResponse<FileContentVO> getFileContentByVersion(@RequestParam("appId") Long appId,
                                                               @RequestParam("fileName") String fileName,
                                                               @RequestParam("version") Integer version,
                                                               HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(appId), ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(ObjUtil.isEmpty(fileName), ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(ObjUtil.isEmpty(version), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(appVersionCtlService.getFileContentByVersion(appId,fileName,version,httpServletRequest));
    }

}
